package cn.com.yusys.yusp.mobile.web.rest;

import cn.com.yusys.yusp.admin.Utils.IDUtils;
import cn.com.yusys.yusp.commons.mapper.QueryModel;
import cn.com.yusys.yusp.commons.web.rest.dto.ResultDto;
import cn.com.yusys.yusp.mobile.service.CustomerVisitService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import sun.misc.BASE64Encoder;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping({"/mobile/customerVisitResource","/api/customerVisitResource"})
public class CustomerVisitResource {

    @Resource
    CustomerVisitService customerVisitService;

    @Value("${filmUrl.testUrl}")
    private String filmUrl;


    //查询客户走访列表
    @GetMapping("/getCustomerVisitList")
    public ResultDto<Object> getCustomerVisitList(QueryModel queryModel) {
        return new ResultDto<>(customerVisitService.getCustomerVisitList(queryModel));
    }
    //查询客户详情
    @GetMapping("/getCustomerDetail")
    public ResultDto<Object> getCustomerDetail(QueryModel queryModel) {
        return new ResultDto<>(customerVisitService.getCustomerDetail(queryModel));
    }
    //查询客户走访详情
    @GetMapping("/getCustomerInterview")
    public ResultDto<Object> getCustomerInterview(QueryModel queryModel) {
        return new ResultDto<>(customerVisitService.getCustomerInterview(queryModel));
    }
    @PostMapping("/updateCustomerInterview")
    public ResultDto<Integer> updateCustomerInterview(@RequestBody Map map) {
        return new ResultDto<Integer>(customerVisitService.updateCustomerInterview(map));
    }

    /**
     * 将内网URL转换为base64图片数据
     *
     * @param url
     * @return byte[]
     */
    public static String getFileBytesFromHttpUrl(String url) {
        HttpURLConnection conn = null;
        ByteArrayOutputStream out = null;
        InputStream in = null;
        byte[] result = null;
        try {
            out = new ByteArrayOutputStream();
            conn = (HttpURLConnection) new URL(url).openConnection();
            conn.setDoInput(true);
            conn.connect();
            in = conn.getInputStream();

            byte[] buff = new byte[128];
            int flag = -1;
            while ((flag = in.read(buff)) != -1) {
                out.write(buff, 0, flag);
            }
            result = out.toByteArray();
        } catch (MalformedURLException e) {
        } catch (IOException e) {
        } finally {
            try {
                in.close();
                out.close();
                conn.disconnect();
            } catch (IOException ignore) {
            }
        }
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(result);
    }
    /**
     * 获取图片
     */
    @GetMapping("/getPhoto")
    public ResultDto<Map> getPhoto(QueryModel queryModel) {
        //service.getFiles(request, this.json);
        Map map=new HashMap();
        SunEcmFileInfoQryService sunEcmFileInfoQryService = new SunEcmFileInfoQryService();
        // 初始化影像平台服务 ip,port 组名
        sunEcmFileInfoQryService.setGroupname("SunECMDM");
        //测试
        sunEcmFileInfoQryService.setIp(filmUrl);
        //生产
//        sunEcmFileInfoQryService.setIp("192.168.220.120");
        sunEcmFileInfoQryService.setSocketport(8022);
        try {
            List<Map<String,String>> arrlist=new ArrayList<Map<String,String>>();
            HashMap yxparams = new HashMap();
            //影像平台配置信息可迁移
//            yxparams.put("DocId", request.getParameter("docId")); // 影像流水号
            yxparams.put("DocId", (String) queryModel.getCondition().get("actFile")); // 影像流水号
            yxparams.put("ModelCode", "YDYHGTGSHZF");
            yxparams.put("FilePartName", "YDYHGTGSHZFP");
            yxparams.put("SunEcmUserName", "WGHYXXT"); // 账号
            yxparams.put("SunEcmPassword", "qwer1234"); // 密码
            yxparams.put("FileType", null);
            Map resultMap = sunEcmFileInfoQryService.SunEcmFileInfoQry(yxparams);
            // 打印返回信息
            List<Map> fileList = (List) resultMap.get("List");
            System.out.println(fileList);
            if (fileList.size() > 0) {
                System.out.println(fileList);
                for (Map<String,Object> info : fileList) {
                    Map<String,String> fileMap=new HashMap<String,String>();
                    fileMap.put("base64",((String) info.get("FileUrl")));
                    arrlist.add(fileMap);
                }
            }
            map.put("arrlist",arrlist);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return  new ResultDto(map);
    }

}
